package com.ampaiva.organizescreen.model;

import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Region;

import com.ampaiva.organizescreen.controller.MoveList;

public class Node implements MoveList.IMovieListElement {
	private Region parent;
	private Region region;
	private Point touched; // if Node is touched/picked up
	private final int order;
	private final int numColumnsX;
	private int marginX;
	private int marginY;
	private int id;

	public Node(int order, int numColumnsX) {
		this.order = order;
		this.numColumnsX = numColumnsX;
	}

	public void setDimensions(int marginX, int marginY, Region parent, int left, int top, int right, int bottom) {
		this.marginX = marginX;
		this.marginY = marginY;
		this.parent = parent;
		region = new Region(left, top, right, bottom);
		int dx = checkBoundsX(0), dy = checkBoundsY(0);
		move(dx, dy);
	}

	private int col_w(int margin) {
		return getRegion().getBounds().right - getRegion().getBounds().left + margin;
	}

	private int col_h(int margin) {
		return getRegion().getBounds().bottom - getRegion().getBounds().top + margin;
	}

	public boolean isTouched() {
		return getTouched() != null;
	}

	public int getPosition() {
		int col_numberx = getColumnX();
		int col_numbery = getColumnY();
		int position = col_numberx + col_numbery * numColumnsX;
		return position;
	}

	public boolean isInRightPosition() {
		return getPosition() == getOrder();
	}

	private void align(int col_numberx, int col_numbery) {
		int x = (col_numberx * col_w(marginX)) + marginX;
		int y = (col_numbery * col_h(marginY)) + marginY;
		getRegion().set(x, y, x + getRegion().getBounds().right - getRegion().getBounds().left,
		        y + getRegion().getBounds().bottom - getRegion().getBounds().top);
	}

	public void align() {
		int col_numberx = getColumnX();
		int col_numbery = getColumnY();
		align(col_numberx, col_numbery);
	}

	private int getColumnY() {
		return getRegion().getBounds().top / col_h(marginY);
	}

	private int getColumnX() {
		return getRegion().getBounds().left / col_w(marginX);
	}

	private int checkBoundsX(int delta) {
		if (getRegion().getBounds().right > parent.getBounds().right)
			delta = parent.getBounds().right - getRegion().getBounds().right;
		else if (getRegion().getBounds().left < parent.getBounds().left)
			delta = parent.getBounds().left - getRegion().getBounds().left;
		return delta;

	}

	private int checkBoundsY(int delta) {
		if (getRegion().getBounds().top < parent.getBounds().top)
			delta = parent.getBounds().top - getRegion().getBounds().top;
		else if (getRegion().getBounds().bottom > parent.getBounds().bottom)
			delta = parent.getBounds().bottom - getRegion().getBounds().bottom;
		return delta;

	}

	public void move(int dx, int dy) {
		if (dx == 0 && dy == 0)
			return;
		Rect r = getRegion().getBounds();
		dx = checkBoundsX(dx);
		dy = checkBoundsY(dy);
		getRegion().set(r.left + dx, r.top + dy, r.right + dx, r.bottom + dy);
	}

	public Region getRegion() {
		return region;
	}

	public Point getTouched() {
		return touched;
	}

	public void setTouched(Point touched) {
		this.touched = touched;
	}

	public int getOrder() {
		return order;
	}

	@Override
	public void setPosition(int position) {
		int col_numberx = position % numColumnsX;
		int col_numbery = position / numColumnsX;
		align(col_numberx, col_numbery);
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}
}
